package cc.mrbird.web.controller;

import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.controller.BaseController;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.system.domain.OneStudent;
import cc.mrbird.web.service.OneStudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class OneStudentController extends BaseController {

	private static final Logger log = LoggerFactory.getLogger(OneStudentController.class);

	@Autowired
	private OneStudentService oneStudentService;

	@Value("${sys.admin}")
	private String admin;

	@Log("获取五年一贯制考生信息")
	@RequestMapping("one_student")
	@RequiresPermissions("one_student:list")
	public String index() {
		return "web/one_student/one_student";
	}

	@RequestMapping("oneStudent/list")
	@ResponseBody
	public Map<String, Object> oneStudentList(QueryRequest request, OneStudent oneStudent,String sonUserName) {
		List<OneStudent> list;
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		if(oneStudent.getUserName().equalsIgnoreCase(admin) && sonUserName == null){
			oneStudent.setUserName("");
			list = this.oneStudentService.findOneStudentByCondition(oneStudent);
		}else if(oneStudent.getUserName().equalsIgnoreCase(admin) && sonUserName != null){
			oneStudent.setUserName(sonUserName);
			list = this.oneStudentService.findOneStudentByCondition(oneStudent);
		}else{
			list = this.oneStudentService.findOneStudentByCondition(oneStudent);
		}
		PageInfo<OneStudent> pageInfo = new PageInfo<>(list);
		return getDataTable(pageInfo);
	}

	@RequestMapping("oneStudent/getOneStudent")
	@ResponseBody
	public ResponseBo getUser(String oneStudentId) {
		try {
			OneStudent oneStudent = this.oneStudentService.findById(oneStudentId);
			return ResponseBo.ok(oneStudent);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("获取用户信息失败，请联系网站管理员！");
		}
	}

	@Log("新增考生信息")
	@RequiresPermissions("one_student:add")
	@RequestMapping("oneStudent/add")
	@ResponseBody
	public ResponseBo addOneStudent(OneStudent oneStudent,String oneUserName) {
		try {
			this.oneStudentService.addOneStudent(oneStudent,oneUserName);
			return ResponseBo.ok("新增考生成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("新增考生失败，请检查考生号或身份证号是否重复！");
		}
	}

	@Log("修改考生信息")
	@RequiresPermissions("one_student:update")
	@RequestMapping("oneStudent/update")
	@ResponseBody
	public ResponseBo updateOneStudent(OneStudent oneStudent) {
		try {
			this.oneStudentService.updateOneStudent(oneStudent);
			return ResponseBo.ok("修改考生成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("修改考生失败，请联系网站管理员！");
		}
	}

	@Log("删除考生")
	@RequiresPermissions("one_student:delete")
	@RequestMapping("oneStudent/delete")
	@ResponseBody
	public ResponseBo deleteOneStudents(String ids) {
		try {
			this.oneStudentService.deleteOneStudents(ids);
			return ResponseBo.ok("删除考生成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("删除考生失败，请联系网站管理员！");
		}
	}

	@RequestMapping("oneStudent/checkStudentId")
	@ResponseBody
	public boolean checkOneStudentId(String oneStudentId, String oldoneStudentId) {
		if (StringUtils.isNotBlank(oldoneStudentId) && oneStudentId.equalsIgnoreCase(oldoneStudentId)) {
			return true;
		}
		OneStudent result = this.oneStudentService.findById(oneStudentId);
		if (result != null)
			return false;
		return true;
	}

	@RequestMapping("oneStudent/checkIdNumber")
	@ResponseBody
	public boolean checkOneIdNumber(String idNumber, String oldidNumber) {
		if (StringUtils.isNotBlank(oldidNumber) && idNumber.equalsIgnoreCase(oldidNumber)) {
			return true;
		}
		OneStudent result = this.oneStudentService.findByIdNumber(idNumber);
		if (result != null)
			return false;
		return true;
	}

}
